/**
 * user-mgr.js
 */
define('url-mgr',['jquery','base','liger.all'],function(require, exports, module){
	var Base = require("base");
	var $ = require("jquery");
	require("liger.all")($);
	var ctx = Base.common.utils.getContextPath();
	
	var $form = $("#form").ligerForm({
		                 inputWidth: 170, labelWidth: 90, space: 40,
		                  fields: [
		                  { name: "id", type: "hidden" },
		                  { display: "名称", name: "name", newline: true, type: "text" , group: "Url信息", groupicon: 'groupicon'}, 
		                  { display: "url", name: "url", newline: false, type: "text"},
		                  { display: "类型", name: "type", newline: true, type: "select", 
		                	  options:{
		                		  data : [{text:'按钮',id:'1'},{text:'页面',id:'2'},{text:'链接',id:'3'},{text:'默认',id:'0'}]}
		                  } ,
		                  { display: "上级", name: "parentId", newline: false, type: "select", 
		                	  options:{
			                      width: 180,
			                      selectBoxWidth: 200,
			                      selectBoxHeight: 200, valueField: 'id',textFiel:'name', 
			                      tree: { url: ctx + '/url/queryForChileren.do?id=0', ajaxType: 'post',
			                    	  single:true,
			                    	  textFieldName:'name',
			                    	  idFieldName:'id',
			                    	  checkbox:false,
			                    	  isLeaf : function(data){
			                              if (!data) return false;
			                              return data.type == "parentId";
			                          },delay: function(e){
			                              var data = e.data;
			                              return { url: ctx + '/url/queryForChileren.do?id=' + data.id };
			                          }
			                      }
		                	  }}]
						}); 
	var param = {};
	
	var $dataGrid = $("#dataGrid").ligerGrid({
		url: ctx + "/url/fetchTree.do",
		root:'datas',
        columns: [ 
        { display: '主键', hide:true, name:'id', align: 'left', width: 120 } ,
        { display: '名称', name: 'name',id:"name", minWidth: 150 },
        { display: 'url', name: 'url', width: 150, align: 'left'},
        { display: '上级', name: 'parent_id', width: 150, align: 'left'},
        { display: '类型', name: 'type', width: 150, align: 'center',
          render: function (item){
        	  var typeName = "";
        	  switch ( item.type ) {
				case 0:  typeName = '默认'; break;
				case 1:  typeName = '按钮'; break;
				case 2:  typeName = '页面'; break;
				case 3:  typeName = '链接'; break;
        	  }
        	  return   typeName;
        }}
        ], data: [], pageSize: 20, sortName: 'id',
        width: '98%', height: '98%', checkbox: true, rownumbers:false,
        fixedCellHeight: false, tree: { columnId: 'name' },
        toolbar:{items: [{text: '查询', click: query, icon:'search'}, { line:true },
                           { text: '编辑', click: modify , icon:'modify'}, { line:true },
                           { text: '删除', click: del, icon:'delete' } ]
                 }
    });
	
	/**
	 * 查询
	 */
	function query(){
    	var form = liger.get("form");
    	var $gridManager = $("#dataGrid").ligerGetGridManager();
    	$gridManager.set({ parms:{param:liger.toJSON(form.getData())} });
    	$gridManager.loadData();
		
	} 
	
	/**
	 * 修改
	 */
	function modify(){
		
    	var row = $dataGrid.getSelectedRow();
    	var url = ctx + "/url/toEdit.do";
    	if(row && row.id){
    		url += "?id=" + row.id;
    	}
    	$.ligerDialog.open({ url:url, width: 500, height: 440, title:"编辑" }); 
	}
	
	/**
	 * 删除记录
	 */
	function del(){
		
		$.ligerDialog.confirm('是否删除所选记录', function (yes){
			if(yes){
				var rows =  $dataGrid.getSelectedRows();
				var param = {user:liger.toJSON(rows)};
		        $.ajax({
		    		url: ctx+ '/url/delete.do',
		    		type : 'POST',
		    		dataType : 'json',
		    		data : param,
		    		success : function(backData) {
		    			if(backData){
		    				$.ligerDialog.tip({  title: '提示信息',content:'记录已经删除！'});
		    				$dataGrid.reload();
		    			}
		    		}
		        });
			}
		
		});
	
	}

    $("#pageloading").hide();
    

});